package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.List;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseSteps;
import top.jnaw.jee.utils.Html2Test;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Steps extends BaseSteps<Steps> {

  public static final Steps dao = new Steps().dao();

  public static ModelResult add(int kid, String title,
      int num) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    Steps s = new Steps();
    if (kid > 0) {
      s.setKid(kid);
      s.setNum(num);
      s.setTitle(title);
      boolean r = s.save();
      if (r) {
        result.setResult(Result.kSuccess);
        result.setObject(s);
      }
    }
    return result;
  }

  public static ModelResult update(int sid, String title,
      int num) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    if (sid > 0) {
      Steps s = Steps.dao.findById(sid);
      if (null != s) {
        s.setNum(num);
        s.setTitle(title);
        boolean r = s.update();
        if (r) {
          result.setResult(Result.kSuccess);
          result.setObject(s);
        }
      }
    }
    return result;
  }

  public static boolean delete(int kid) {
    Db.update("delete from steps where kid=" + kid);
    return true;
  }

  public static boolean deleteSid(int sid) {
    Db.update("delete from steps where id=" + sid);
    return true;
  }

  public static JSONObject list(int kid, String openid) {

    String sec = Db.getSql("step-list-sec");
    String exp = Db.getSql("step-list-exp");
//    if (StrKit.notBlank(openid)) {
//      exp +=
//          "\n" + " AND ks.openid='" + openid + "'";
//    }
    Page<Record> pages = Db.paginate(1, 100, sec, exp, kid);
    return Models.compressPage(pages);
  }

  public static List<Steps> getList(int kid, String openid) {

    String sql = Db.getSql("step-getList-sec");
    List<Steps> list;
    list = dao.find(sql, kid);

    return list;
  }

  public static List<Steps> getListByOpenid(int kid, String openid) {

    String sql = Db.getSql("step-getListOpenid-sec");
    List<Steps> list;
    list = dao.find(sql, kid, openid);

    return list;
  }

  public static List<Steps> getListByKid(int kid) {
    String sql = Db.getSql("step-getAllListByKid-sec");
    List<Steps> list;
    list = dao.find(sql, kid);
    return list;
  }

  public static List<Steps> getAllListByKid(int kid, String openid) {
    String sql = Db.getSql("step-getAllListByKid-sec");
    List<Steps> list;
    list = dao.find(sql, kid);
    return list;
  }

  public static List<Steps> getMyListByKid(int kid, String openid,
      String status) {
    String sql = Db.getSql("step-getMyListByKid-sec");
    List<Steps> list;
    list = dao.find(sql, kid, openid, status);
    return list;
  }


}
